"""
Internationalization module for PDF Splitter.
"""

from .translator import Translator
from .languages import (
    SUPPORTED_LANGUAGES,
    DEFAULT_LANGUAGE,
    detect_system_language,
    get_language_name,
    get_language_native_name,
)

# Global translator instance
_translator = None

def set_global_translator(translator):
    """Set the global translator instance."""
    global _translator
    _translator = translator

def get_global_translator():
    """Get the global translator instance."""
    global _translator
    return _translator

def tr(text: str, *args):
    """
    Global translation function.
    This function should be called from all UI components that need translation.

    Args:
        text: Text to translate with placeholders like %1, %2, etc.
        *args: Values to replace placeholders

    Returns:
        Translated text as string
    """
    global _translator
    if _translator and hasattr(_translator, '_current_language'):
        from .simple_translations import translate as simple_translate
        result = simple_translate(text, _translator._current_language)
        # Replace placeholders
        for i, arg in enumerate(args, 1):
            result = result.replace(f"%{i}", str(arg))
        return result
    # Return original text with placeholders if no translator
    result = text
    for i, arg in enumerate(args, 1):
        result = result.replace(f"%{i}", str(arg))
    return result

__all__ = [
    'Translator',
    'tr',  # The main translation function
    'SUPPORTED_LANGUAGES',
    'DEFAULT_LANGUAGE',
    'detect_system_language',
    'get_language_name',
    'get_language_native_name',
    'set_global_translator',
    'get_global_translator',
]